local fenwei = fk.CreateSkill {

  name = "joyex__fenwei",

  tags = { Skill.Limited, },

}



fenwei:addEffect(fk.TargetSpecified, {
  name = "joyex__fenwei",
  anim_type = "defensive",
  frequency = Skill.Limited,
  events = {fk.TargetSpecified,fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
      return player:hasSkill(fenwei.name) and data.card.type == Card.TypeTrick and #data:getAllTargets() > 1 and
        player:usedSkillTimes(fenwei.name, Player.HistoryGame) == 0
    elseif event == fk.AfterCardsMove and player:hasSkill(fenwei.name) and player:isKongcheng() and player:usedSkillTimes("joyex__fenwei", Player.HistoryGame) > 0 then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then return true end
    local tos = player.room:askForChoosePlayers(player, data:getAllTargets(),
      1, 10, "#fenwei-choose:::"..data.card:toLogString(), fenwei.name, true)
    if #tos > 0 then
      event:setCostData(self, tos)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
      player:setSkillUseHistory("joyex__fenwei", 0, Player.HistoryGame)
    else
      table.insertTable(data.nullifiedTargets, event:getCostData(self))
      if not player.dead then
        player:drawCards(1,fenwei.name)
      end
    end
  end,
})
fenwei:addEffect(fk.AfterCardsMove, {
  name = "joyex__fenwei",
  anim_type = "defensive",
  frequency = Skill.Limited,
  events = {fk.TargetSpecified,fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
      return player:hasSkill(fenwei.name) and data.card.type == Card.TypeTrick and #data:getAllTargets() > 1 and
        player:usedSkillTimes(fenwei.name, Player.HistoryGame) == 0
    elseif event == fk.AfterCardsMove and player:hasSkill(fenwei.name) and player:isKongcheng() and player:usedSkillTimes("joyex__fenwei", Player.HistoryGame) > 0 then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then return true end
    local tos = player.room:askForChoosePlayers(player, data:getAllTargets(),
      1, 10, "#fenwei-choose:::"..data.card:toLogString(), fenwei.name, true)
    if #tos > 0 then
      event:setCostData(self, tos)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
      player:setSkillUseHistory("joyex__fenwei", 0, Player.HistoryGame)
    else
      table.insertTable(data.nullifiedTargets, event:getCostData(self))
      if not player.dead then
        player:drawCards(1,fenwei.name)
      end
    end
  end,
})

return fenwei